package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustormServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public PageInfo<Customer> selectByQo(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Customer> list = customerMapper.selectAll(qo);
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void saveOrUpdate(Customer customer) {
        if (customer.getId() == null) {
            //获取当前用户  即录入人员信息
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            customer.setInputUser(employee);
            //设置当前修改或保存时间
            customer.setInputTime(new Date());
            customerMapper.insert(customer);
        } else {
            customerMapper.updateByPrimaryKey(customer);
        }
    }

    @Override
    public void updateStatus(Long customerId, Integer status) {
        customerMapper.updateStatus(customerId,status);
    }

    @Override
    public void updateSellerIdByCustomerId(Long sellerId, Long customerId) {
        customerMapper.updateSellerIdByCustomerId(sellerId,customerId);
    }
}
